package com.szpx.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.szpx.entity.DdwUser;
import com.szpx.mapper.DdwUserMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@Service
public class DdwUserService {
    @Resource
    private  DdwUserMapper ddwUserMapper;
    @Resource
    private  MailService mailService;



    public Map<String, Object> createAccount(DdwUser ddwUser){
        //雪花算法生成确认码
        String confirmCode = IdUtil.getSnowflake(1,1).nextIdStr();
        //生成盐
        String salt = RandomUtil.randomString(6);
        //加密密码
        String md5Pwd = SecureUtil.md5(ddwUser.getPassword()+salt);
        //激活失效时间
        LocalDateTime ldt = LocalDateTime.now().plusDays(1);
        //初始化账号信息
        ddwUser.setSalt(salt);
        ddwUser.setPassword(md5Pwd);
        ddwUser.setConfirmCode(confirmCode);
        ddwUser.setActivationTime(ldt);
        ddwUser.setIsValid((byte)0);
        //新增账号
        int result = ddwUserMapper.inserUser(ddwUser);
        Map<String, Object> resultMap =new HashMap<>();
        if (result >0){
            //发送邮箱
            String activationUrl = "http://127.0.0.1:8086/user/activation?confirmCode="+confirmCode;
            mailService.sendMailForActivationAccount(activationUrl,ddwUser.getEmail());

            resultMap.put("code",200);
            resultMap.put("message","注册成功：前往邮箱激活");
        }else {
            resultMap.put("code",400);
            resultMap.put("message","注册失败");
        }
        return resultMap;
    }

    public Map<String,Object> loginAccout(DdwUser ddwUser) {
        Map<String, Object> resultMap = new HashMap<>();
        //根据邮箱查询用户
        List<DdwUser> ddwUserList = ddwUserMapper.selectUerByEmail(ddwUser.getEmail());
        //查询不到结果返回：该账户不存在或未激活
        if (ddwUserList == null || ddwUserList.isEmpty()) {
            resultMap.put("code", 400);
            resultMap.put("message", "账户不存在或未激活");
            return resultMap;
        }
        //查询多个用户，返回：账号异常：联系管理员
        if (ddwUserList.size() > 1) {
            resultMap.put("code", 400);
            resultMap.put("message", "账号异常：联系管理员");
            return resultMap;
        }

        //密码比对
        DdwUser u = ddwUserList.get(0);
        //用户输入的密码和盐对比
        String md5Pwd = SecureUtil.md5(ddwUser.getPassword() + u.getSalt());
        if (!u.getPassword().equals(md5Pwd)){
            resultMap.put("code",400);
            resultMap.put("message","用户名和密码不一致");
            return resultMap;

    }
        //密码不一致，返回用户名和密码
        resultMap.put("code",200);
        resultMap.put("message","登录成功");
        return resultMap;
    }

    public Map<String, Object> activationAccount(String confirmCode) {
        Map<String ,Object> resultMap = new HashMap<>();
        DdwUser ddwUser = ddwUserMapper.selectUserByConfirmCode(confirmCode);
        //时间
        boolean after = LocalDateTime.now().isAfter(ddwUser.getActivationTime());
        if (after){
            resultMap.put("code",400);
            resultMap.put("message","连接失效,请重试");
            return resultMap;
        }
        int result = ddwUserMapper.updateUserByConfirmCode(confirmCode);
        if (result>0){
            resultMap.put("code",200);
            resultMap.put("message","激活成功，进行登录");
            return resultMap;
        }
        return resultMap;
    }

    }








   /*
   public  Map<String, Object> activationAccount(String confirmCode) {
        Map<String ,Object> resultMap = new HashMap<>();
        DdwUser ddwUser = ddwUserMapper.selectUserByConfirmCode(confirmCode);
        //时间
        boolean after = LocalDateTime.now().isAfter(ddwUser.getActivationTime());
        if (after){
            resultMap.put("code",400);
            resultMap.put("message","连接失效,请重试");
            return resultMap;
        }
        int result = ddwUserMapper.updateUserByConfirmCode(confirmCode);
        if (result>0){
            resultMap.put("code",200);
            resultMap.put("message","激活失败");
            return resultMap;
        }
        return resultMap;
    }
    */






